package com.xhwl.centiotdata.handler.device.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.lifang.DeviceLiFangDTO;
import com.xhwl.centiotdata.pojo.dto.lifang.DoorLiFangDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.lifang.DeviceLiFangHandler;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.pedes.DeviceAttributeDTO;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 16:22 2022/7/11
 */
@Slf4j
@Service
public class DeviceIntegrationLiFangHandler extends DeviceIntegrationHandler {

    @Autowired
    private IDeviceCentService deviceCentService;
    @Autowired
    private DeviceLiFangHandler deviceLiFangHandler;

    @Override
    public void afterPropertiesSet() throws Exception {
        DeviceIntegrationFactory.register(ProductEnum.LIFANG_OCS.getId(), this);
    }

    @Override
    public void pullDevice(DeviceIntegrationConfigDTO configDTO) {
        switch (configDTO.getDeviceType()) {
            case 1:
                this.pullDoor(configDTO);
                break;
            default:
        }

    }

    public void pullDoor(DeviceIntegrationConfigDTO configDTO){
        if (configDTO.getDeviceType() == null) {
            log.error("projectId：{} pullDevice failed! deviceType is null!", configDTO.getProjectId());
            return;
        }
        // 拉取数据
        List<DoorLiFangDTO> doorLiFangDTOList = deviceLiFangHandler.pullDoor(configDTO);
        if (CollectionUtils.isEmpty(doorLiFangDTOList)) {
            return;
        }
        JSONObject status = deviceLiFangHandler.pullDeviceStatus(configDTO);
        Map<String,Integer> map = new HashMap<>();
        Iterator<String>  keyIterator =  status.keySet().iterator();
        while (keyIterator.hasNext()){
            String doorId = keyIterator.next();
            JSONObject statusJson = status.getJSONObject(doorId);
            String statusInt = statusJson.keySet().iterator().next();
            map.put(doorId,statusInt.equals("3")?0:1);
        }
        log.info("门状态：{}",JSONObject.toJSONString(map));
        // 转换数据
        List<DeviceDTO> deviceDTOList = this.handleDevice(configDTO, doorLiFangDTOList,map);
        // 存储业务中台
        deviceCentService.asyncUpdateDevice(deviceDTOList);
    }

    public void pullFaceDevice(DeviceIntegrationConfigDTO configDTO){
        List<DeviceLiFangDTO> deviceLiFangDTOS = deviceLiFangHandler.pullFaceDevice(configDTO);
        if (CollectionUtils.isEmpty(deviceLiFangDTOS)) {
            return;
        }
        List<DeviceDTO> deviceDTOList = this.handleFaceDevice(configDTO, deviceLiFangDTOS);
    }

    @Override
    public void pullDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        super.pullDeviceOnlineStatus(configDTO);
    }

    private List<DeviceDTO> handleDevice(DeviceIntegrationConfigDTO configDTO,List<DoorLiFangDTO> doorLiFangDTOList,Map<String,Integer> map ){
        return doorLiFangDTOList.stream().map(doorLiFangDTO -> {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setProjectId(configDTO.getProjectId());
            deviceDTO.setProductId(configDTO.getProductId());
            deviceDTO.setCategoryId(configDTO.getCategoryEnum()==null? CategoryEnum.DOOR.getId():configDTO.getCategoryEnum().getId());
            deviceDTO.setDeviceTypeId(configDTO.getDeviceType());
            deviceDTO.setName(doorLiFangDTO.getDoorName());
            deviceDTO.setThirdDeviceId(doorLiFangDTO.getDoorId().toString());
            deviceDTO.setIotDeviceId(doorLiFangDTO.getDevicesysid().toString());
            //获取设备在线状态
            deviceDTO.setOnlineStatus(CollectionUtils.isEmpty(map)?1:(map.containsKey(doorLiFangDTO.getDoorId().toString())?map.get(doorLiFangDTO.getDoorId().toString()):1));
            JSONObject attributeJson = new JSONObject();
            deviceDTO.setAttributeJson(attributeJson);
            return deviceDTO;
        }).collect(Collectors.toList());
    }

    private List<DeviceDTO> handleFaceDevice(DeviceIntegrationConfigDTO configDTO,List<DeviceLiFangDTO> deviceLiFangDTOList ){
        return deviceLiFangDTOList.stream().map(deviceLiFangDTO -> {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setProjectId(configDTO.getProjectId());
            deviceDTO.setProductId(configDTO.getProductId());
            deviceDTO.setCategoryId(configDTO.getCategoryEnum()==null? CategoryEnum.DOOR.getId():configDTO.getCategoryEnum().getId());
            deviceDTO.setDeviceTypeId(configDTO.getDeviceType());
            deviceDTO.setName(deviceLiFangDTO.getStationName());
            deviceDTO.setThirdDeviceId(deviceLiFangDTO.getStationNo());
//            deviceDTO.setOnlineStatus(deviceLiFangDTO.getIsDeviceOnline() ? DeviceOnlineStatusEnum.ONLINE.id : DeviceOnlineStatusEnum.OFFLINE.id);
            JSONObject attributeJson = null;
            if (configDTO.getDeviceType().intValue() == 1) {
                DeviceAttributeDTO deviceAttributeDTO = new DeviceAttributeDTO();
                deviceAttributeDTO.setIp(deviceLiFangDTO.getStationIp());
                attributeJson = (JSONObject) JSONObject.toJSON(deviceAttributeDTO);
            }
            deviceDTO.setAttributeJson(attributeJson);
            return deviceDTO;
        }).collect(Collectors.toList());
    }
}
